#!/bin/bash
#
# Author:   Dr. Mike Murphy (mmurphy2@coastal.edu)
# Revision: 11 November 2013
#
#   Copyright 2012-2013 Coastal Carolina University
#
#   Licensed under the Apache License, Version 2.0 (the "License");
#   you may not use this file except in compliance with the License.
#   You may obtain a copy of the License at
#
#       http://www.apache.org/licenses/LICENSE-2.0
#
#   Unless required by applicable law or agreed to in writing, software
#   distributed under the License is distributed on an "AS IS" BASIS,
#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#   See the License for the specific language governing permissions and
#   limitations under the License.
#


## p_agent_hostname_redhat_set_hostname
##
## Modifies /etc/sysconfig/network and the appropriate network-scripts files
## to change the hostname setting for a Red Hat system. Returns 0 on
## success, non-zero on error.
##
function p_agent_hostname_redhat_set_hostname() {
	local status=0
	local chk=0
	
	hostname "${rq_hostname}"
	
	sed -i s/'HOSTNAME=.*'/"HOSTNAME=$1"/ "${P_ROOT}/etc/sysconfig/network"
	status=$?
	for s in `ls /etc/sysconfig/network-scripts/ifcfg-*`; do
		if [[ "${s}" != "${P_ROOT}/etc/sysconfig/network-scripts/ifcfg-lo" ]]
		then
			sed -i s/^DHCP_HOSTNAME=.*//g "${s}" > /dev/null 2>&1
			chk=$?
			[ ${status} -eq 0 ] && status=${chk}
			echo "DHCP_HOSTNAME=$1" >> "${s}"
		fi
	done
	return ${status}
}
